package parser

import (
	"learning/goclac/tokens"
	"strconv"
)

type tStackNodeType int
const NODE_VALUE tStackNodeType = 1
const NODE_TOKEN tStackNodeType = 2

type tStackNode struct {
	flag tStackNodeType
	token *tokens.Token
	value float64
}


func newValueNode(value float64) *tStackNode {
	return &tStackNode{
		flag: NODE_VALUE,
		value: value,
		token: nil,
	}
}


func newTokenNode(token *tokens.Token) *tStackNode {
	return &tStackNode{
		flag: NODE_TOKEN,
		token: token,
		value: 0,
	}
}


func (me *tStackNode) getTokenType() tokens.TOKENS {
	switch me.flag {
	case NODE_VALUE:
		return tokens.DoubleLiteral

	case NODE_TOKEN:
		switch me.token.Token {
		case tokens.IntLiteral:
			fallthrough
		case tokens.DoubleLiteral:
			return tokens.DoubleLiteral
		default:
			return me.token.Token
		}
	}

	return tokens.UNKNOWN
}


func (me *tStackNode) getDoubleValue() float64 {
	switch me.flag {
	case NODE_VALUE:
		return me.value

	case NODE_TOKEN:
		switch me.token.Token {
		case tokens.IntLiteral:
			fallthrough
		case tokens.DoubleLiteral:
			v1,e1 := strconv.ParseFloat(me.token.Value, 64)
			if e1 != nil {
				panic(e1)
			}
			return v1
		}
	}

	panic("value not avaiable")
}